




	
#define INIT_PREV_KERNEL	0
#define INIT_POST_KERNEL	1
#define INIT_DRIV_INTERF	2
#define INIT_APPLICATION	3
#define INIT_END			4
#define INIT_MAX			5

#define MAX_PRIORITIES		7
#define DEFAULT_TASK_SIZE		128


#define tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB 		( ( UINT8 ) 0 )
#define tskSTATICALLY_ALLOCATED_STACK_ONLY 			( ( UINT8 ) 1 )
#define tskSTATICALLY_ALLOCATED_STACK_AND_TCB		( ( UINT8 ) 2 )

#define taskNOT_WAITING_NOTIFICATION				( ( UINT8 ) 0 )
#define taskWAITING_NOTIFICATION					( ( UINT8 ) 1 )
#define taskNOTIFICATION_RECEIVED					( ( UINT8 ) 2 )


/* FreeRTOS error definitions. */
#define errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY		( -1 )
#define errQUEUE_BLOCKED							( -4 )
#define errQUEUE_YIELD								( -5 )




#define HEAP_SIZE				(SIZE_T)(2048)								// 8192 4byte
#define BlockAllocatedBit		(SIZE_T)(0x8000)

#define BlockStructStandard		((sizeof(BlockLink_t) + (SIZE_T)0x0007) & ~((SIZE_T)0x0007 ))



#define AlignCheck(x)			if(x == CLEAR)						{ vPortRaiseBASEPRI(); for( ;; );}
#define CoupleCheck(x)			if(x == CLEAR)						{ vPortRaiseBASEPRI(); for( ;; );}
#define AlignSet(RequestSize)	if((RequestSize & 0x0007) != CLEAR)	{ RequestSize += (8 - (RequestSize & 0x0007));}







#define STRINGIFY(s)							_STRINGIFY(s)
#define _DO_CONCAT(x, y)						x ## y
#define _CONCAT(x, y)							_DO_CONCAT(x, y)
#define DEVICE_NAME_GET(name)					(_CONCAT(WROS_DEV_, name))


struct xLIST_ITEM
{
	volatile UINT32 xItemValue;				// volatile may can unuse
	struct xLIST_ITEM * volatile pxNext;		
	struct xLIST_ITEM * volatile pxPrevious;	
	void * pvOwner;										
	void * volatile pvContainer;				
};
typedef struct xLIST_ITEM ListItem_t;					



struct xMINI_LIST_ITEM
{
	volatile UINT32 xItemValue;				// volatile may can unuse
	struct xLIST_ITEM * volatile pxNext;
	struct xLIST_ITEM * volatile pxPrevious;
};
typedef struct xMINI_LIST_ITEM MiniListItem_t;



typedef struct xLIST
{
	volatile UINT32 uxNumberOfItems;		// volatile may can unuse
	ListItem_t * volatile pxIndex;
	MiniListItem_t xListEnd;
} List_t;


typedef struct tskTaskControlBlock
{
//	xMPU_SETTINGS	xMPUSettings;		// The MPU settings are defined as part of the port layer.  THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT.
	ListItem_t	xStateListItem;			// The list that the state list item of a task is reference from denotes the state of that task (Ready, Blocked, Suspended ).
	ListItem_t	xEventListItem;			// Used to reference a task from an event list.
	UINT32*		pxStack;				// Points to the start of the stack.
	volatile UINT32* pxTopOfStack;		// Points to the location of the last item placed on the tasks stack.  THIS MUST BE THE FIRST MEMBER OF THE TCB STRUCT.
	volatile UINT32* pxEndOfStack;		// Points to the highest valid address for the stack.
	UINT32		uxPriority;				// The priority of the task.  0 is the lowest priority.
	UINT32		uxBasePriority;			// The priority last assigned to the task - used by the priority inheritance mechanism.
	UINT8		pcTaskName[16];			// Descriptive name given to the task when created.  Facilitates debugging only. lint !e971 Unqualified char types are allowed for strings and single characters only.
	UINT32		uxCriticalNesting;		// Holds the critical section nesting depth for ports that do not maintain their own count in the port layer.
	UINT32		uxMutexesHeld;
	UINT32		ulRunTimeCounter;		// Stores the amount of time the task has spent in the Running state.
	volatile	UINT32 ulNotifiedValue;
	volatile	UINT8 ucNotifyState;
	UINT8		ucDelayAborted;
	UINT32		uxTCBNumber;			// Stores a number that increments each time a TCB is created.  It allows debuggers to determine when a task has been deleted and then recreated.
	UINT32		uxTaskNumber;			// Stores a number specifically for use by third party trace code. */
} tskTCB;
typedef tskTCB TCB_t;


typedef struct BLOCK_LINK
{
	struct BLOCK_LINK* pNextFreeBlock;		/*<< The next free block in the list. */
	UINT32 BlockSize;						/*<< The size of the free block. */
} BlockLink_t;


struct device
{
	const		char* name;
	void 		(*driver_api)(T_DispInfo*);
	UINT32		driver_level;
	UINT32		driver_StackDepth;
	UINT32		driver_ErrorIndex;
	TCB_t*		driver_TaskHandle;
};


#define DEVICE_DEFINE(dev_name, api, level, StackDepth, ErrorIndex, TaskHandle) \
__root struct device DEVICE_NAME_GET(dev_name) @ "WROS_section" = { \
    .name = STRINGIFY(dev_name), \
    .driver_api = api, \
    .driver_level = level, \
    .driver_StackDepth = StackDepth, \
    .driver_ErrorIndex = ErrorIndex, \
    .driver_TaskHandle = TaskHandle, \
};

static BlockLink_t T_Block_Start, *P_Block_End = NULL;

enum
{
	No_Error,
	A1_Error,
	A2_Error,
};

/*
#define FORCE_LINK_BEGIN(dev_name, level) 
#define FORCE_LINK_END(dev_name, level) \
	_Pragma(STRINGIFY(_CONCAT(location=, STRINGIFY(_CONCAT(_CONCAT(__devices_, level), m))))) \
	__root struct device *_CONCAT(__devices_pointer, dev_name) = &_CONCAT(__devices, dev_name);

#define DEVICE_DEFINE(dev_name, level, api, conf, data, init, deinit) \
FORCE_LINK_BEGIN(dev_name, level) \
struct device DEVICE_NAME_GET(dev_name) = \
{ \
	.name = STRINGIFY(dev_name), \
	.driver_api = api, \
	.driver_conf = conf, \
	.driver_data = data, \
	.driver_init = init, \
	.driver_deinit = deinit, \
}; \
FORCE_LINK_END(dev_name, level)
*/

void RUN_IDLE(T_DispInfo* psDisplayInfo)	
{
	//WD->CR = IP_WDT_CLEARCODE;
	//gucWdt_Nmi_Count = CLEAR;	
};


DEVICE_DEFINE(TimerTask,	WROS_TimerTask,			INIT_PREV_KERNEL,	DEFAULT_TASK_SIZE,	No_Error, NULL)
DEVICE_DEFINE(PLATFORMCOMM, App_SetPlatform,		INIT_POST_KERNEL,	DEFAULT_TASK_SIZE,	No_Error, NULL)
DEVICE_DEFINE(DISPLAY,		Cmp_Display_Service,	INIT_DRIV_INTERF,	DEFAULT_TASK_SIZE,	No_Error, NULL)
DEVICE_DEFINE(ERRORCHECK,	Cmp_ErrorCheck,			INIT_DRIV_INTERF,	DEFAULT_TASK_SIZE,	No_Error, NULL)
DEVICE_DEFINE(TaskIDLE, 	RUN_IDLE,				INIT_APPLICATION,	DEFAULT_TASK_SIZE,	No_Error, NULL)
DEVICE_DEFINE(LQCPROCESS,	Run_LQC_ModeProcess,	INIT_APPLICATION,	DEFAULT_TASK_SIZE,	No_Error, NULL)


